import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import { PureComponent } from "react";
import "./_style.less";
import prettier from "prettier/standalone";
import parserHtml from "prettier/parser-html";
import { Input } from "antd";
import { parseFormulaByContentApi } from "../../cpas-table/ExcelGrid/api/DesignTableApi";
const { TextArea } = Input;
//P标签正则替换
function replacePTagsWithNewLines(str) {
    return str.replace(/<\/p>/g, "<br>").replace(/<p>/g, "");
}
//处理引号
function replaceQuot(str) {
    if (!str) {
        return;
    }
    if (str.includes("quot;")) {
        return str.replace(/&quot;/g, '"');
    }
    return str;
}
/**匹配公式
 * {FS_CELL("base.reportItems","财务费用","账面发生额","2017")}
 * @param str
 * @returns
 */
function matchContent(str) {
    const reg = /(?<=\{)(.*?)(?=\})/g;
    //const regex = /(CP_CELL?.*?\(.+?\))|(FS_CELL:?.*?\(.+?\))|(IN_CELL:?.*?\(.+?\))/g;
    if (!str) {
        return;
    }
    console.log(str, "111");
    return str.match(reg);
}
//公式转换
function replaceFormula(str, replacements) {
    const regex = /{([^}]+)}/g;
    if (!str) {
        return false;
    }
    //str = replacePTagsWithNewLines(str);
    return str.replace(regex, function (match, key) {
        return replacements.get(key) ? replacements.get(key) : "--";
    });
}
class CpasEditor extends PureComponent {
    htmlEditor = React.createRef(); //容器Ref
    constructor(props) {
        super(props);
        this.state = {
            content: "",
            preview: "", //预览内容
        };
        // this.htmlEditor.current.instance.getText();
        this.valueChanged = this.valueChanged.bind(this);
        this.handleClickOutside = this.handleClickOutside.bind(this);
        this.prettierFormat = this.prettierFormat.bind(this);
        this.onFocusOut = this.onFocusOut.bind(this);
    }
    valueChanged(e) {
        console.log(e.target.value, "e.target.value-----");
        this.setState({
            content: e.target.value,
        });
    }
    setData(data) {
        this.setState({
            content: data,
        });
    }
    getData() {
        return this.state.content;
    }
    getPreviewData() {
        return this.state.preview;
    }
    setPreviewData(data) {
        this.setState({
            preview: data,
        });
    }
    async handleClickOutside(e) {
        console.log("handleClickOutside", this.state.content);
        const { content } = this.state;
        const { tableRef } = this.props;
        const displayData = tableRef.current.hot?.getData();
        try {
            const preview_response = await parseFormulaByContentApi({
                content,
                displayData,
            });
            if (preview_response.ok) {
                this.setState({
                    preview: preview_response.data,
                });
            }
        }
        catch (e) {
            console.error(e);
        }
    }
    componentDidMount() { }
    componentWillUnmount() { }
    onFocusOut(e) {
        this.handleClickOutside(e);
        if (this.props.onFocusOut) {
            this.props.onFocusOut(e);
        }
    }
    prettierFormat() {
        const { preview } = this.state;
        if (!preview) {
            return;
        }
        return prettier.format(preview, {
            parser: "html",
            plugins: [parserHtml],
        });
    }
    render() {
        return (_jsx("div", { className: "cpas-editor", children: _jsx(TextArea, { rows: 2, ref: this.htmlEditor, onChange: this.valueChanged, value: this.state.content, onBlur: this.onFocusOut, ...this.props }) }));
    }
}
export default CpasEditor;
